package demo.todo;

import act.controller.annotation.UrlContext;
import act.db.jpa.JPADao;
import act.util.JsonView;
import org.osgl.mvc.annotation.DeleteAction;
import org.osgl.mvc.annotation.GetAction;
import org.osgl.mvc.annotation.PostAction;
import org.osgl.util.S;

import java.util.List;
import javax.inject.Inject;

@JsonView
@UrlContext("/todos")
public class TodoService {

    @Inject
    private JPADao<Integer, Todo> dao;

    @GetAction
    public List<Todo> list(String q) {
        return S.blank(q) ?
                dao.findAllAsList() :
                dao.q("desc like", q).fetch();
    }

    @GetAction("{id}")
    public Todo findById(int id) {
        return dao.findById(id);
    }

    @PostAction
    public int create(Todo todo) {
        dao.save(todo);
        return todo.id;
    }

    @DeleteAction("{id}")
    public void remove(int id) {
        dao.deleteById(id);
    }

}
